package com.spring.shopping.service.impl;

import com.spring.shopping.entity.User;
import com.spring.shopping.mapper.UserMapper;
import com.spring.shopping.service.UserService;
import com.spring.shopping.util.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/*
 * @Coding By ZTK:
 */

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public void registerUser(User user) {

        user.setPassword(PasswordUtil.encrypt(user.getPassword()));
        userMapper.insertUser(user);
    }

    @Override
    public User loginUser(String username, String password) {
        User user = userMapper.selectUserByUsername(username);
        if (user != null && PasswordUtil.check(password, user.getPassword())) {
            return user;
        }
        return null;
    }

    @Override
    public User getUserById(Integer id) {
        return userMapper.selectUserById(id);
    }
}